package com.runen.wnhz.runen.common.login.Wx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;

import com.bumptech.glide.util.Util;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;


/**
 * 描述：
 * 作者： 天天童话丶
 * 时间： 2018/5/13.
 */

public class WeiXinUtils {

    private static WeiXinUtils utils;
    private IWXAPI mWxApi;
    private WXCallBack mCallBack;
    private WxProxyActivity mWxProxyActivity;
    private final String WxAppID = "wx45ded9c0b63a0889";
    private final String WxSecret = "6c940d7cdd924baf004111c24fbb0a04";

    private WeiXinUtils(){}

    public static WeiXinUtils getInstance(){
        if (utils == null){
            synchronized (WeiXinUtils.class){
                if (utils == null){
                    utils = new WeiXinUtils();
                }
            }
        }

        return utils;
    }

    // 注册
    public WeiXinUtils regToWx(Context context) {
        //通过WXAPIFactory工厂获取IWXApI的示例
        mWxApi = WXAPIFactory.createWXAPI(context, WxAppID, true);
        //将应用的appid注册到微信
        mWxApi.registerApp(WxAppID);
        return this;
    }

    // 登录
    public WeiXinUtils wxLogin(Activity activity, String wxToken, String wxOpenId, String wxRefreshToken){
        Intent intent = new Intent(activity, WXLoginActivity.class);
        intent.putExtra("WxToken", wxToken);
        intent.putExtra("WxOpenId", wxOpenId);
        intent.putExtra("WxRefreshToken", wxRefreshToken);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
        return this;
    }

    // 支付
    public void wxPay(WeiXinPayBean weiXinPayBean){
        PayReq req = new PayReq();

        req.appId = weiXinPayBean.getAppid();
        req.partnerId = weiXinPayBean.getPartnerid();
        req.prepayId = weiXinPayBean.getPrepayid();
        req.nonceStr = weiXinPayBean.getNoncestr();
        req.timeStamp = weiXinPayBean.getTimestamp() + "";
        req.packageValue = "Sign=WXPay";
        req.sign = weiXinPayBean.getSign();

        mWxApi.sendReq(req);
    }

    public WxProxyActivity getWxProxyActivity() {
        return mWxProxyActivity;
    }

    public void setWxProxyActivity(WxProxyActivity mWxProxyActivity) {
        this.mWxProxyActivity = mWxProxyActivity;
    }

    public WXCallBack getWxCallBack() {
        return mCallBack;
    }

    public WeiXinUtils setWxCallBack(WXCallBack mCallBack) {
        this.mCallBack = mCallBack;
        return this;
    }

    public IWXAPI getWxApi() {
        return mWxApi;
    }

    public String getWxAppID() {
        return WxAppID;
    }

    public String getWxSecret() {
        return WxSecret;
    }

    public static final int IMAGE_LENGTH_LIMIT = 6291456;

    /*public static ByteArrayOutputStream getWXThumb(Bitmap resource) {
        Bitmap thumb = createScaledBitmap(resource, 100, true);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int quality = 90;
        int realLength = Util.getBitmapByteSize(resource.getWidth(), resource.getHeight(), Bitmap.Config.ARGB_8888);
        if (realLength > IMAGE_LENGTH_LIMIT) {
            quality = (int) (IMAGE_LENGTH_LIMIT * 1f / realLength * 100);
        }
        if (quality < 75) {
            quality = 75;
        }
        resource.compress(Bitmap.CompressFormat.PNG, quality, output);
        output.reset();
        thumb.compress(Bitmap.CompressFormat.PNG, 85, output);
        return output;
    }*/
}
